<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018-10-17
 * Time: 16:38
 */
namespace app\api\model;
use think\Db;

class GetBank
{
    /***随机获取一道符合条件的考题
     * @param $three_level_id
     * @param null $type
     * @param null $difficulty
     * @param int $status
     * @param $array
     * @return array|bool|null|\PDOStatement|string|\think\Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    static public function get($three_level_id,$type=null,$difficulty=null,$status=1,$array){
        if(!$three_level_id){
            return false;
        }

            if($type){
                if($difficulty){
                    if($array){
                        $data = Db::name('bank')
                            ->where('three_level_id',$three_level_id)
                            ->where('type',$type)
                            ->where('difficulty',$difficulty)
                            ->where('status',$status)
                            ->where("id","not in",$array)
                            ->orderRaw("rand()")
                            ->find();
                    }else{
                        $data = Db::name('bank')
                            ->where('three_level_id',$three_level_id)
                            ->where('type',$type)
                            ->where('difficulty',$difficulty)
                            ->where('status',$status)
                            ->orderRaw("rand()")
                            ->find();
                    }

                }else{
                    if($array){
                        $data = Db::name('bank')
                            ->where('three_level_id',$three_level_id)
                            ->where('type',$type)
                            ->where('status',$status)
                            ->where("id","not in",$array)
                            ->orderRaw("rand()")
                            ->find();
                    }else{
                        $data = Db::name('bank')
                            ->where('three_level_id',$three_level_id)
                            ->where('type',$type)
                            ->where('status',$status)
                            ->orderRaw("rand()")
                            ->find();
                    }
                }
            }else{
                if($difficulty){
                    if($array){
                        $data = Db::name('bank')
                            ->where('three_level_id',$three_level_id)
                            ->where('difficulty',$difficulty)
                            ->where('status',$status)
                            ->where("id","not in",$array)
                            ->orderRaw("rand()")
                            ->find();
                    }else{
                        $data = Db::name('bank')
                            ->where('three_level_id',$three_level_id)
                            ->where('difficulty',$difficulty)
                            ->where('status',$status)
                            ->orderRaw("rand()")
                            ->find();
                    }
                }else{
                    if($array){
                        $data = Db::name('bank')
                            ->where('three_level_id',$three_level_id)
                            ->where('status',$status)
                            ->where("id","not in",$array)
                            ->orderRaw("rand()")
                            ->find();
                    }else{
                        $data = Db::name('bank')
                            ->where('three_level_id',$three_level_id)
                            ->where('status',$status)
                            ->orderRaw("rand()")
                            ->find();
                    }
                }
            }
            $data['yuyin'] = urlencode($data['stem']);
            return $data;
    }
}